<?php
namespace PsiFramework\Applications\AlloMediaPortal\Classes\DAO;

import("Plugins.Form.Classes.DAO.FormDAO");

use \PsiFramework\Classes\Model\Object;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Util\Parameters\ObjectParameters;
use \PsiFramework\Plugins\Form\Classes\DAO\FormDAO;

/**
 * AlloMedia
 * Portail des professionnels
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The CategoryLinkDAO is the Data Access Object for the class Theme
 *
 * @package DAO
 */
class ThemeDAO extends FormDAO
{

    protected static $_singleton = null;

    public function delete(
        Object &$object,
        /*bool*/ $performTransaction=true,
        /*bool*/ $checkSecurity=true
    )
    {

        $responseIdDao = DAOFactory::getDAO('ResponseId');
        $informationPartDao = DAOFactory::getDAO('InformationPart');

        if ($performTransaction) {
        	$this->_database->startTransaction();
        }

        $ok = true ;
        $responseIds = $object->getResponseIds();
        foreach ($responseIds as $responseId) {
            $ok = $responseIdDao->delete($responseId, false, $checkSecurity);
            if (!$ok) {
            	break;
            }
        }

        if ($ok) {
            $parts = $object->getParts();
            foreach ($parts as $part) {
                $ok = $informationPartDao->delete($part, false, $checkSecurity);
                if (!$ok) {
                	break;
                }
            }
        }

        if ($ok) {
        	$ok = parent::delete($object, false, $checkSecurity);
        }

        if ($performTransaction) {
            if ($ok) {
                $this->_database->commit();
            } else {
                $this->_database->rollback();
            }
        }

        return $ok ;
    }

    /**
     * Function to be called instead of the constructor.
     *
     * @return ThemeDAO    instance
     */
    public static function getInstance(ObjectParameters $objectParameters)
    {

        if (self::$_singleton==null) {
            self::$_singleton = new ThemeDAO($objectParameters);
        }

        return self::$_singleton;
    }

}
